package cn.xdf.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;
import java.util.Objects;

// 把BindingAwareModelMap中的username属性和所有的Integer类型的属性都存入一份在session中
//存入username，或者integer类型的数据
@SessionAttributes(value = {"username"},types = {Integer.class})
@Controller
@RequestMapping("/demo06")
public class Demo06Controller_SessionAttributes {

    @GetMapping("/demo01")
    public String demo01(Map<String, Object>map, HttpServletRequest request)throws IOException{

        map.put("username","admin");
        map.put("password","123");
        map.put("age",23);
        map.put("tall",1.78);

        //存入request域的值不能被存入session
        request.setAttribute("test",111);

        return "redirect:/demo06/demo02";
    }

    @GetMapping("/demo02")
    public void demo02(HttpSession session, HttpServletResponse response)throws IOException{

        response.setContentType("text/html;charset=utf8");

        response.getWriter().write("username: " + session.getAttribute("username") + "<hr />");
        response.getWriter().write("password: " + session.getAttribute("password") + "<hr />");
        response.getWriter().write("age: " + session.getAttribute("age") + "<hr />");
        response.getWriter().write("tall: " + session.getAttribute("tall") + "<hr />");
        response.getWriter().write("test: " + session.getAttribute("test") + "<hr />");
    }
}
